
package administrador;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.sql.*;
import javax.swing.*; 
import java.awt.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
/**
 *
 * @author 
 */
public class VentKardex extends javax.swing.JPanel {
    Conexion conexion;
    int bodega;
    Usuario user;
    
    /** Creates new form VentKardex */
    public VentKardex(Conexion cEnt,int bEnt,Usuario user) {
        initComponents();
        conexion=cEnt;
        bodega=bEnt;
        this.user=user;
        try{
        creaGrafBarras3D("Ventas Semana Actual","Valores (dolares)","Dias de la Semana",Color.BLACK,Color.red,Color.LIGHT_GRAY,lblchart1,sacoSemana(true));
        creaGrafBarras3D("Ventas Semana Pasada","Valores (dolares)","Dias de la Semana",Color.BLACK,Color.blue,Color.lightGray,lblchart2,sacoSemana(false));
        }
        catch(Exception e){}ValidoOpUsuario(conexion);
    }
    
    
        private void ValidoOpUsuario(Conexion conexion){
     //Quito el boton de borrado para el Vendedor:
       String priv=user.getPrivilegio();
        if(priv.equals("e")){
        

        }
        //*******************************************
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel9 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel14 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txt_fact = new javax.swing.JTextField();
        txt_tact = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txt_fpas = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txt_tpas = new javax.swing.JTextField();
        refrescar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        lblchart1 = new javax.swing.JLabel();
        lblchart2 = new javax.swing.JLabel();

        jLabel9.setText("jLabel9");

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setLayout(null);

        jLabel1.setFont(new java.awt.Font("Stencil", 1, 36)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 102, 102));
        jLabel1.setText("Ventas");
        jPanel2.add(jLabel1);
        jLabel1.setBounds(20, 30, 160, 40);
        jPanel2.add(jSeparator1);
        jSeparator1.setBounds(20, 290, 150, 10);

        jLabel14.setForeground(new java.awt.Color(255, 153, 0));
        jLabel14.setText("Reportes");
        jPanel2.add(jLabel14);
        jLabel14.setBounds(20, 80, 70, 40);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setForeground(new java.awt.Color(153, 0, 0));
        jLabel2.setText("Semana Actual:");
        jPanel2.add(jLabel2);
        jLabel2.setBounds(30, 300, 100, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setForeground(new java.awt.Color(51, 0, 153));
        jLabel3.setText("Semana Pasada:");
        jPanel2.add(jLabel3);
        jLabel3.setBounds(30, 380, 100, 14);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Fecha:");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(30, 400, 50, 14);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Total:");
        jPanel2.add(jLabel5);
        jLabel5.setBounds(30, 340, 50, 14);

        txt_fact.setEditable(false);
        txt_fact.setForeground(new java.awt.Color(51, 0, 204));
        jPanel2.add(txt_fact);
        txt_fact.setBounds(80, 400, 80, 20);

        txt_tact.setEditable(false);
        txt_tact.setForeground(new java.awt.Color(102, 51, 0));
        jPanel2.add(txt_tact);
        txt_tact.setBounds(80, 340, 80, 20);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Fecha:");
        jPanel2.add(jLabel6);
        jLabel6.setBounds(30, 320, 50, 14);

        txt_fpas.setEditable(false);
        txt_fpas.setForeground(new java.awt.Color(102, 51, 0));
        jPanel2.add(txt_fpas);
        txt_fpas.setBounds(80, 320, 80, 20);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Total:");
        jPanel2.add(jLabel7);
        jLabel7.setBounds(30, 420, 50, 14);

        txt_tpas.setEditable(false);
        txt_tpas.setForeground(new java.awt.Color(51, 0, 204));
        jPanel2.add(txt_tpas);
        txt_tpas.setBounds(80, 420, 80, 20);

        refrescar.setText("Refrescar");
        refrescar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refrescarActionPerformed(evt);
            }
        });
        jPanel2.add(refrescar);
        refrescar.setBounds(80, 450, 80, 23);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel8.setText("Historial de Ventas:");
        jPanel1.add(jLabel8);

        jButton1.setText("Generar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        jLabel10.setText("Comprobantes Anulados:");
        jPanel1.add(jLabel10);

        jButton2.setText("Generar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);

        jLabel11.setText("Transacciones Erradas:");
        jPanel1.add(jLabel11);

        jButton3.setText("Generar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);

        jPanel2.add(jPanel1);
        jPanel1.setBounds(20, 110, 150, 170);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblchart1.setFocusTraversalPolicyProvider(true);
        jPanel3.add(lblchart1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 590, 240));
        jPanel3.add(lblchart2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 260, 590, 240));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void refrescarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refrescarActionPerformed
// TODO add your handling code here:
        try{
        creaGrafBarras3D("Ventas Semana Actual","Valores (dolares)","Dias de la Semana",Color.BLACK,Color.red,Color.LIGHT_GRAY,lblchart1,sacoSemana(true));
        creaGrafBarras3D("Ventas Semana Pasada","Valores (dolares)","Dias de la Semana",Color.BLACK,Color.blue,Color.lightGray,lblchart2,sacoSemana(false));
        }
        catch(Exception e){}
        
    }//GEN-LAST:event_refrescarActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    reportesC r=new reportesC(conexion,"historialVentas");
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    report_ComAnulados r=new report_ComAnulados(conexion);
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
    visorErradas ve=new visorErradas();
    
}//GEN-LAST:event_jButton3ActionPerformed
    
    public DefaultCategoryDataset sacoSemana(boolean actual){
        DefaultCategoryDataset datos = new DefaultCategoryDataset();
        
        //Llena los datos desde el ResultSet
        ResultSet r;
        Date contaFecha[] =new Date[2];
        r=conexion.consultarBase("SELECT fecha_pe as Semanas, DAYNAME(fecha_pe) as Dia, SUM(total_pagado) as SUMA FROM pedido  where DAYNAME(fecha_pe) like 'Monday'   GROUP BY fecha_pe order by fecha_pe;");
        
        
        System.out.println("����������������$$$$$$$$$$$$$$����������������������");
         try{
             r.last();
             r.previous();
             r.previous();
             
             int i=0;
             while(r.next()){
                System.out.println(r.getDate(1));
                System.out.println(r.getString(2));
                System.out.println(r.getDouble(3));
                //Consulto los dias de esta semana:
                contaFecha[i]=r.getDate(1);
                i++;
                
            }
        }catch(SQLException sqle){
            System.out.println("Error al mostrar los resultados de la consulta");
            System.err.println(sqle);
        }
        txt_fact.setText(contaFecha[0].toString());
        txt_fpas.setText(contaFecha[1].toString());
        if(!actual){
        //Saca semana Pasada:
        r=conexion.consultarBase("SELECT fecha_pe as Semanas, DAYNAME(fecha_pe) as Dia, SUM(total_pagado) as SUMA FROM pedido  where fecha_pe>='"+contaFecha[0]+"' and fecha_pe<'"+contaFecha[1]+"' GROUP BY fecha_pe order by fecha_pe;");
        double total=0;
        
        System.out.println("����������������0000000000000����������������������");
         try{
             r.beforeFirst();
             
             while(r.next()){
                Date fecha=r.getDate(1);
                String dia=r.getString(2);
                double valor=r.getDouble(3);
                //Consulto los dias de esta semana:
                datos.setValue(valor,"Profit1",dia);
                total=total+valor;
            }
             
        }catch(SQLException sqle){
            System.out.println("Error al mostrar los resultados de la consulta");
            System.err.println(sqle);
        }
        //Pongo Totales:
         total=total*100;
         total=Math.round(total);
         total=total/100;
         txt_tpas.setText(String.valueOf(total));
        System.out.println("����������������00000000000����������������������");
            
        }else
        {
        //Saca Semana Actual:
        r=conexion.consultarBase("SELECT fecha_pe as Semanas, DAYNAME(fecha_pe) as Dia, SUM(total_pagado) as SUMA FROM pedido  where fecha_pe>='"+contaFecha[1]+"'  GROUP BY fecha_pe order by fecha_pe;");
        double total=0;
        System.out.println("����������������111111111111����������������������");
         try{
             r.beforeFirst();
             
             while(r.next()){
                Date fecha=r.getDate(1);
                String dia=r.getString(2);
                double valor=r.getDouble(3);
                //Consulto los dias de esta semana:
                datos.setValue(valor,"Profit1",dia);
                total=total+valor;
            }
             
        }catch(SQLException sqle){
            System.out.println("Error al mostrar los resultados de la consulta");
            System.err.println(sqle);
        }
        //Pongo Totales:
         total=total*100;
         total=Math.round(total);
         total=total/100;
         txt_tact.setText(String.valueOf(total));
        System.out.println("����������������111111111111����������������������");
            
            
        }
        
        
        return datos;
    }
    
    
//    public DefaultCategoryDataset semanaPasada(){
//        DefaultCategoryDataset datos;
//        return datos;
//    }
    
    public void creaGrafBarras3D(String titulo,String ejex,String ejey,Color fondo_Color,Color titulo_Color,Color tapiz,javax.swing.JLabel lbl_etiqueta,DefaultCategoryDataset datos){
   //Crea un Grafico3D de Barras:
        //Especifica los datos a crear en un dataset.
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset=datos;
               
        //Genera el grafico segun los Datos.
        JFreeChart chart = ChartFactory.createBarChart3D(   
                titulo,  //Title
                ejey,            //etiqueta eje x
                ejex,                    //etiqueta eje y
                dataset,                        //Dataset
                PlotOrientation.VERTICAL,       //Orientacion del Plot
                false,                           //Muestra Leyenda
                true,                           //Usa tooltips
                false                           //Configura el chart para generar URLS?
                                                        );
        
        //Le guarda  a la grafica como una imagen JPG:
//        try {
//            ChartUtilities.saveChartAsJPEG(new File("C:\\chart.jpg"), chart, 610, 150);
//        } catch (Exception e) {
//            System.out.println("Problem occurred creating chart.");
//        }
        //Personaliso:
       chart.setBackgroundPaint(tapiz); // Color Fondo 
       chart.getTitle().setPaint(titulo_Color); // Color Titulo Color.blue
       CategoryPlot pc = chart.getCategoryPlot(); // Toma el objeto plot para un grafico de barra
       pc.setBackgroundPaint(fondo_Color); // Modifica el Fondo del PlotColor.black
       pc.setRangeGridlinePaint(Color.red); //Modifica el Color lineas Grilla
       
       //Carga la imagen Generada en un Label:
       
       
           //Creo un bufer de imagenes y le pongo en una etiqueta
           BufferedImage image = chart.createBufferedImage(565,240);
           lbl_etiqueta.setIcon(new ImageIcon(image));
           setVisible(true);
      
       
   }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblchart1;
    private javax.swing.JLabel lblchart2;
    private javax.swing.JButton refrescar;
    private javax.swing.JTextField txt_fact;
    private javax.swing.JTextField txt_fpas;
    private javax.swing.JTextField txt_tact;
    private javax.swing.JTextField txt_tpas;
    // End of variables declaration//GEN-END:variables
    
}
